﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using Demo.Services.Integration.DAO.Impl;
using Demo.CommonBase.Logging;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.DTO.Filters;
using Demo.Services.Contracts.Exceptions;

namespace Demo.Services.Services.Impl.DomainObjects
{
    public class OperationDomainObject
    {
        public OperationPagingInfo GetOperations(OperationFilter filter)
        {
            OperationPagingInfo result = null;
            OperationDAO operationDao;
            try
            {
                Logger.Info();
                operationDao = new OperationDAO();
                result = operationDao.GetOperations(filter);
            }
            catch (AbstractException)
            {
                throw;
            }  
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public ResourcePagingInfo GetOperationResources(ResourceFilter filter)
        {
            ResourcePagingInfo result = null;
            OperationDAO operationDao;
            try
            {
                Logger.Info();
                operationDao = new OperationDAO();
                result = operationDao.GetOperationResources(filter);
            }
            catch (AbstractException)
            {
                throw;
            }  
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public ResourceInfo GetResourceLineTime(int id)
        {
            ResourceInfo result = null;
            OperationDAO operationDao;
            TecnologyDomainObject tecnology;
            try
            {
                Logger.Info();
                operationDao = new OperationDAO();
                result = operationDao.GetResourceLineTime(id);
                if (result != null)
                {
                    tecnology = new TecnologyDomainObject();
                    result.TecnologyList = tecnology.GetTecnologysByResource(id);
                }
            }
            catch (AbstractException)
            {
                throw;
            }  
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public void  SaveLineTime(LineTimeInfo lineTime)
        {
            OperationDAO operationDao;
            try
            {
                Logger.Info();
                //validar que fecha de asignacion esten en el rango de fecha del proyecto
                operationDao = new OperationDAO();
                if (operationDao.ValidationOvarlapDateResource(lineTime))
                {
                    throw new DemoServiceException(DemoServiceException.RANGOFECHA_EXCEPTION);
                }

                operationDao = new OperationDAO();
                operationDao.SaveLineTime(lineTime);
                
            }
            catch (AbstractException)
            {
                throw;
            }  
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }            
        }

        public void  UpdateLineTime(LineTimeInfo lineTime)
        {
            OperationDAO operationDao;
            try
            {
                Logger.Info();
                operationDao = new OperationDAO();
                operationDao.UpdateLineTime(lineTime);
                
            }
            catch (AbstractException)
            {
                throw;
            }  
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            
        }

        public void DeleteLineTime(int id)
        {
            OperationDAO operationDao;
            try
            {
                Logger.Info();
                operationDao = new OperationDAO();
                operationDao.DeleteLineTime(id);                
            }
            catch (AbstractException)
            {
                throw;
            }  
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }            
        }

        public void DeleteOperationLineTime(int id)
        {
            OperationDAO operationDao;
            try
            {
                Logger.Info();
                operationDao = new OperationDAO();
                operationDao.DeleteOperationLineTime(id);
            }
            catch (AbstractException)
            {
                throw;
            }  
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

    }
}
